package smartcampus.scjava.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import smartcampus.scjava.POJO.DO.LibrarySeatReserve;
import smartcampus.scjava.custom.SeatTimeEnum;
import smartcampus.scjava.mapper.LibrarySeatReserveMapper;
import smartcampus.scjava.service.ILibrarySeatReserveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 座位预定表 服务实现类
 * </p>
 *
 * @author xian
 * @since 2023-11-22
 */
@Service
public class LibrarySeatReserveServiceImpl extends ServiceImpl<LibrarySeatReserveMapper, LibrarySeatReserve> implements ILibrarySeatReserveService {

    @Override
    public List<Map<String, Object>> listReserveBySeat(String seatId) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<LibrarySeatReserve> librarySeatReserves = baseMapper.selectReserveBySeatId(seatId);
        if (CollectionUtils.isNotEmpty(librarySeatReserves)) {
            Map<Integer, Integer> map = new HashMap<>();
            for (LibrarySeatReserve librarySeatReserve : librarySeatReserves) {
                LocalDateTime beginTime = librarySeatReserve.getBeginTime();
                LocalDateTime endTime = librarySeatReserve.getEndTime();
                int startHour = beginTime.getHour();
                int startIndex = SeatTimeEnum.getIndex(startHour);
                if (startIndex == 0) {
                    continue;
                }
                int endTimeHour = endTime.getHour();
                for (int i = 0; i < endTimeHour - startHour; i++) {
                    int index = SeatTimeEnum.getIndex(startHour+i);
                    map.put(index,1);
                }
            }
            for (int i = 1; i <= 14; i++) {
                Integer s = map.get(i);
                Map<String,Object> indexMap = new HashMap<>();
                if(s == null) {
                    indexMap.put("time", SeatTimeEnum.getHour(i));
                    indexMap.put("status", 0);
                } else {
                    indexMap.put("time", SeatTimeEnum.getHour(i));
                    indexMap.put("status", s);
                }
                list.add(indexMap);
            }
        }
        return list;
    }

    @Override
    public boolean checkRepeatSeatReserve(Integer seatId,String[] times) {
        List<Map<String, Object>> listMaps = listReserveBySeat(String.valueOf(seatId));
        Map<String,Integer> checkMap = new HashMap<>();
        for (Map<String, Object> map : listMaps) {
            Integer status = (Integer) map.get("status");
            if (status == 1) {
                checkMap.put(String.valueOf(map.get("time")),status);
            }
        }
        for (String time : times) {
            Integer i = checkMap.get(time);
            if (i != null) {
                return false;
            }
        }
        return true;
    }


}
